package cn.jizi.bshisy.controller;

import cn.jizi.bshisy.entity.Register;
import cn.jizi.bshisy.service.RegisterService;
import cn.jizi.bshisy.tool.PageSupport;
import cn.jizi.bshisy.tool.ShuiJi;
import com.sun.istack.logging.Logger;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/register")
public class RegisterController {
    private Logger logger = Logger.getLogger(RegisterController.class);
    @Resource
    private RegisterService registerService;

    //病人挂号
    @ApiOperation(value = "添加一条病人挂号信息", notes = "添加一条病人挂号信息")
    @PostMapping(value = "/getAddRegister")
    public String getAddRegister(Register register) {
        register.setRid(ShuiJi.random());
        int count = registerService.getAddRegister(register);
        if (count > 0) {
            return "挂号成功";
        } else {
            return "挂号失败";
        }
    }

    //根据收费项目类型，病人姓名查询
    @ApiOperation(value = "医生查看挂号信息并可以根据病人姓名和时间查询并分页", notes = "医生查看挂号信息并可以根据病人姓名和时间查询并分页")
    @GetMapping(value = "/getRegisterListPage")
    public List<Register> getRegisterListPage(Model model, String name, String pageIndex) {
        logger.info("==========================================》进入");
        logger.info("name==================>" + name);
        logger.info("pageIndex==================>" + pageIndex);
        List<Register> registerList = null;
        Integer diagnose=3;
        // 设置页面容量
        int pageSize = 3;
        // 当前页码
        int currentPageNo = 1;
        // 设置页码数
        if (pageIndex != null) {
            currentPageNo = Integer.valueOf(pageIndex);
        }
        // 总数量（表）
        int totalCount = 0;
        try {
            totalCount = registerService.getListRegisterPageCount(name,diagnose);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 总页数
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        // 控制首页和尾页
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        try {

            registerList = registerService.getListRegisterPage(name,diagnose,currentPageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("registerList========================" + registerList);
        model.addAttribute("pages", pages);
        model.addAttribute("registerList", registerList);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("currentPageNo", currentPageNo);
        model.addAttribute("name", name);
        return registerList;
    }
}
